import sys, secrets, hashlib, base58, sha3
from ecdsa import SigningKey, SECP256k1

def ripemd160(data):
    h = hashlib.new('ripemd160')
    h.update(data)
    return h.digest()

# 从种子私钥转换成钱包格式
def privateKey2WIF(payload,version): 
    s =  b'\x80' + bytes.fromhex(payload)
    if version == "btc": s = s + b'\x01'
    checksum = hashlib.sha256(hashlib.sha256(s).digest()).digest()[0:4]
    result = base58.b58encode(s + checksum)
    return result

# 从私钥生成压缩公钥
def pri2pubcom(private_key_string):
    sk = SigningKey.from_string(bytes.fromhex(private_key_string), curve=SECP256k1)
    vk = sk.get_verifying_key()
    x = vk.to_string()[:32]
    y = vk.to_string()[32:]
    if y[-1] % 2 == 0:
        prefix = b'\x02'
    else:
        prefix = b'\x03'
    return (prefix + x).hex()

# 从私钥生成公钥
def pri2pub(private_key_string):
    sk = SigningKey.from_string(bytes.fromhex(private_key_string), curve=SECP256k1)
    vk = sk.get_verifying_key()
    x = vk.to_string()[:32]
    y = vk.to_string()[32:]
    prefix = b'\x04'    
    return (prefix + x + y).hex()

# 从公钥生成BTC公钥地址
def pub2addressbtc(public_key_string):  
    sha256_digest = hashlib.sha256(bytes.fromhex(public_key_string)).digest()
    h160 = ripemd160(sha256_digest)    
    pubkeyhash = b'\x00' + h160
    checksum_pub = hashlib.sha256(hashlib.sha256(pubkeyhash).digest()).digest()[:4]
    address = base58.b58encode(pubkeyhash + checksum_pub)
    return address

# 从公钥生成EOS公钥地址
def pub2addresseos(public_key_string):    
    h160 = ripemd160(bytes.fromhex(public_key_string))
    checksum = h160[:4]
    address = 'EOS' + base58.b58encode(bytes.fromhex(public_key_string) + checksum).decode()
    return address
    
# 从公钥生成ETH地址
def pub2addresseth(public_key_string):
    _hash = sha3.keccak_256()
    address = public_key_string[2:]
    _hash.update(bytes.fromhex(address))
    address = "0x"+_hash.hexdigest()[24:]
    return address

# 从ETH地址转换成TRX地址
def pub2addresstrx(public_key_string):    
    address1 = '41' + public_key_string[2:]
    checksum_pub = hashlib.sha256(hashlib.sha256(bytes.fromhex(address1)).digest()).digest()[:4]
    address = base58.b58encode(bytes.fromhex(address1) + checksum_pub).decode()
    return address

if __name__ == '__main__':
    # 种子私钥
    private_key = hex(secrets.randbits(256))[2:]
    if len(sys.argv) > 1:
        if len(sys.argv[1]) == 64:  
            private_key = sys.argv[1]
    # EOS私钥
    private_key_wif_eos = privateKey2WIF(private_key,"eos")
    # BTC私钥
    private_key_wif_btc = privateKey2WIF(private_key,"btc")
    # BTC公钥
    public_key = pri2pubcom(private_key)
    # ETH公钥
    public_key_eth = pri2pub(private_key)
    # EOS公钥地址
    address_eos = pub2addresseos(public_key)
    # BTC地址
    address_btc = pub2addressbtc(public_key)
    # ETH地址
    address_eth = pub2addresseth(public_key_eth)
    # TRX地址
    address_trx = pub2addresstrx(address_eth)
    print("种子私钥：", private_key,"\n")
    print("BTC私钥：", str(private_key_wif_btc,"utf-8"))
    print("BTC公钥：", public_key)
    print("BTC地址：", str(address_btc,"utf-8"),"\n")        
    print("EOS私钥：", str(private_key_wif_eos,"utf-8"))    
    print("EOS公钥：", address_eos,"\n")
    print("ETH私钥：", private_key)
    print("ETH地址：", address_eth,"\n")
    print("TRX私钥：", private_key)
    print("TRX地址：", address_trx)